package com.example.test.advice.第十一章_开源世界.建议140_推荐使用Guava工具包;

import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.*;
import com.google.common.primitives.Ints;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Client {

    @Test
    public void test1(){
        //不可变列表
        ImmutableList<String> list = ImmutableList.of("A","B","C");
        System.out.println("list:"+list);
        //不可变Map
        ImmutableMap<Integer,String> map = ImmutableMap.of(1,"壹",2,"贰",3,"叁");
        System.out.println("map:"+map);
    }

    @Test
    public void test2(){
        //多值Map
        Multimap<String,String> phoneBook = ArrayListMultimap.create();
        phoneBook.put("张三","110");
        phoneBook.put("张三","119");
        System.out.println(phoneBook.get("张三").stream().collect(Collectors.toList()));
    }

    @Test
    public void test3() {
        //table表
        Table<Double, Double, String> g = HashBasedTable.create();
        //定义人民工厂的经纬度坐标
        g.put(31.23, 121.48, "人民广场");
        g.put(31.23,121.48,"万达广场");//当第一个参数和第二个参数与上一个元素重复时将进行覆盖第三个参数的值
        //输入坐标电的建筑物
        System.out.println("坐标对应地址：" + g.get(31.23, 121.48));
    }

    @Test
    public void test4(){
        //姓名、年龄键值对
        Map<String,Integer> user = new HashMap<>();
        user.put("张三",20);
        user.put("李四",22);
        user.put("王五",25);
        //所有年龄大于20岁的人员
        Map<String,Integer> filtedMap = Maps.filterValues(user, integer -> integer>20);//过滤函数

        System.out.println(filtedMap);
    }

    //字符串操作
    @Test
    public void test5(){
        //定义链接符号
        Joiner joiner = Joiner.on(",");
        //可以连接多个对象，不局限与String ；如果有null，则跳过
        String str = joiner.skipNulls().join("嘿","Guava很不错的");
        System.out.println("str:"+str);
        Map<String,String> map = new HashMap<>();
        map.put("张三","普通员工");
        map.put("李四","领导");
        //键之间以‘是’连接，多个键值以空格分割
        System.out.println(Joiner.on("\r\n").withKeyValueSeparator("是").join(map));
    }

    @Test
    public void test6(){
        String str = "你好，Guava";
        //以中文逗号分隔
        for(String s: Splitter.on(",").split(str)){
            System.out.println(s);
        }
        System.out.println("========================");
        //按照固定长度分隔
        for(String s:Splitter.fixedLength(2).split(str)){
            System.out.println(s);
        }
    }

    //基本类型工具包
    @Test
     public void test7(){
        int[] ints = {10,9,20,40,80};
        //从数组中取出最大值
        System.out.println(Ints.max(ints));
        List<Integer> integers = new ArrayList<>();
        //把包装类型的集合转为基本类型数组
        ints = Ints.toArray(integers);
        System.out.println("ints:"+ints);
    }
}
